public class Philosophy extends Thread{
    private static int counter = 0;
    private int number = counter++;
    private Chopsticks left;
    private Chopsticks right;
    public Philosophy(Chopsticks oleft,Chopsticks oright){
        this.left = oleft;
        this.right = oright;
        start();
    }
    public void think(){
        try{
            sleep(33);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void eat(){
        synchronized (this.left){
            String cur = this.toString();
            System.out.println(cur +"持有"+ this.left + "等待"+this.right);
            synchronized (this.right){
                System.out.println(cur+"左手持有"+ this.left + "右手持有"+this.right);
                System.out.println(this+"-------------吃菜----------");
            }
        }
    }
    public String toString(){
        return "哲学家："+ this.number;
    }
    public void run(){
        while(true){
            think();
            eat();
        }
    }
}
